//
//  HMTabBar.swift
//  weibo
//
//  Created by LCH on 16/3/3.
//  Copyright © 2016年 lch. All rights reserved.
//

import UIKit

class HMTabBar: UITabBar {
    // 闭包 无法回调
    var composeButtonClickClouse:(()->())?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
        
    }
    
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
     addSubview(composeButton)
    
    
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let itemW = self.frame.width/5
        var index = 0
        
        for value in self.subviews {
  // 这个类型为什么是UITabBarButton类型,这边设循环的作用
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            
              let x = CGFloat(index) * itemW
              value.frame.size.width = itemW
              value.frame.origin.x = x
               
                index++
                
                if index == 2 {
                
                    index++
                }
                
            }
        
        
        }
        composeButton.center = CGPoint(x:self.center.x, y: self.bounds.height * 0.5)
        
    }
    
    func composeButtonClick() {
    
      composeButtonClickClouse?()
    
    }
    
    
    
    private lazy var composeButton: UIButton = {
    
      let button = UIButton()
        
        // 设置不同状态下的背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        // 设置不同状态下的图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
    
        button.sizeToFit()
        
        return button
        
    
    }()
    
    
    
    
}
